/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller;


import hibernate.model.dao.KhachHangDAO;
import hibernate.pojo.Khachhang;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import net.tanesha.recaptcha.ReCaptchaImpl;
import net.tanesha.recaptcha.ReCaptchaResponse;
/**
 *
 * @author phathuynh
 */
@WebServlet(name = "registrationcontroller2", urlPatterns = {"/registrationcontroller2"})
public class registrationcontroller2 extends HttpServlet {

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            request.setCharacterEncoding("UTF-8");
            HttpSession session = request.getSession();
            String action = request.getParameter("action");
            if (action != null) 
            {
                if (action.equals("registration2")) 
                {
                    request.getRequestDispatcher("/pagestile/registration2.jsp").forward(request, response);
                }
                //<editor-fold defaultstate="collapsed" desc="Sử dụng goolge captcha">
                if (action.equals("register2")){
                    String tenDangNhap = request.getParameter("tendangnhap");
                    String matKhau = request.getParameter("matkhau");
                    String passEncrypted = KhachHangDAO.encryptPassword(matKhau);
                    String xacNhanMatKhau = request.getParameter("xacnhanmatkhau");
                    String soDiDong = request.getParameter("sodidong");
                    String eMail = request.getParameter("email");
                    
                    String ho = request.getParameter("ho");
                    String tenDem = request.getParameter("tendem");
                    String ten = request.getParameter("ten");
                    String hoVaTen = ho + tenDem + ten;
                    String gioiTinh = request.getParameter("gioitinh");
                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy/mm/dd");
                    String strNgaySinh = request.getParameter("ngay");
                    String strThangSinh = request.getParameter("thang");
                    String strNamSinh = request.getParameter("nam");
                    String ntnSinh = strNgaySinh +"/"+ strThangSinh +"/"+ strNamSinh;
                    Date ngaySinh = new Date();
                    try {
                        ngaySinh = sdf.parse(ntnSinh);
                    } catch (ParseException ex) {
                        Logger.getLogger(registrationcontroller2.class.getName()).log(Level.SEVERE, null, ex);
                    }
                    String diaChi = request.getParameter("diachi");
                    String thanhPho = request.getParameter("thanhpho");
                    String tinhTrangHonNhan = request.getParameter("tinhtranghonnhan");
                    
                    if(!matKhau.equals(xacNhanMatKhau)){
                        request.getRequestDispatcher("/pagestile/registration.jsp").forward(request, response);
                    }

                    Khachhang khachHang = new Khachhang();
                    khachHang.setTaiKhoan(tenDangNhap);
                    khachHang.setMatKhau(passEncrypted);
                    khachHang.setSoDienThoai(soDiDong);
                    khachHang.setEmail(eMail);
                    khachHang.setHoTen(hoVaTen);
                    khachHang.setGioiTinh(gioiTinh);
                    khachHang.setNgaySinh(ngaySinh);
                    khachHang.setTinhTrangHonNhan(tinhTrangHonNhan);
                    khachHang.setDiaChi(diaChi);
                    khachHang.setThanhPho(thanhPho);

                    String remoteAddr = request.getRemoteAddr();
                    ReCaptchaImpl reCaptcha = new ReCaptchaImpl();
                    reCaptcha.setPrivateKey("6Ldo38gSAAAAAN3mO85IhEC7HUrVLhAQtL-kLaHW");

                    String challenge = request.getParameter("recaptcha_challenge_field");
                    String uresponse = request.getParameter("recaptcha_response_field");
                    ReCaptchaResponse reCaptchaResponse = reCaptcha.checkAnswer(remoteAddr, challenge, uresponse);

                    if (reCaptchaResponse.isValid()) {
                        boolean result = KhachHangDAO.register(khachHang);
                            if(result == true){
                                request.getRequestDispatcher("logincontroller?action=login").forward(request, response);
                            }
                            else {
                                request.getRequestDispatcher("/pagestile/registration.jsp").forward(request, response);
                            }
                    } else {
                        request.getRequestDispatcher("/pagestile/registration.jsp").forward(request, response);
                    }
                }
                //</editor-fold>
            }
            
        } finally {            
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
